<template>
  <button
    class="btn-submit btn-primary w-100 size-normal"
    :class="{ 'is-fail': isFail }"
    type="button"
  >
    <transition name="submit-btn-label" mode="out-in">
      <div v-if="isLoading" key="loading" class="btn-submit-wrapper">
        <div class="spinner"></div>
      </div>
      <div
        v-else-if="isSuccess"
        key="success"
        class="btn-submit-wrapper"
      >
        <div class="btn-submit-text">
          提交成功
        </div>
      </div>
      <div v-else-if="isFail" key="fail" class="btn-submit-wrapper">
        <div class="btn-submit-text">
          提交失败
        </div>
      </div>
      <div v-else key="default" class="btn-submit-wrapper">
        <div class="btn-submit-text">
          {{ text }}
        </div>
        <span class="btn-submit-icon"></span>
      </div>
    </transition>
  </button>
</template>

<script lang="ts" setup>
defineOptions({
  name: "BtnSubmit"
});

interface Props {
  text?: string;
  isLoading?: boolean;
  isSuccess?: boolean;
  isFail?: boolean;
}

withDefaults(defineProps<Props>(), {
  text: "Button",
  isLoading: false,
  isSuccess: false,
  isFail: false
});
</script>

<style lang="scss" scoped>
.w-100 {
  width: 100% !important
}

.btn-submit {
  position: relative;
  border: none;
  padding: 11px 15px;
  min-width: 160px;
  border-radius: 4px;
  background-color: var(--text-color--primary, #{#0032A0});
  box-shadow: 0px 2px 6px rgba(130, 130, 140, 0.12);
  overflow: hidden;
  z-index: 1;
}

.btn-submit:before {
  content: '';
  display: block;
  background: #0048E8;
  position: absolute;
  width: 200%;
  height: 500%;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  transition: all .3s ease-in-out;
  z-index: -1;
  clip-path: circle(100% at -80% 90%);
}

.btn-submit.is-fail {
  background-color: #ff8200;
}

.btn-submit.is-fail:before {
  background: #ff8200;
}
.btn-submit-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  white-space: nowrap;
}

.btn-submit:hover {
  &:before {
    clip-path: circle(100% at 50% 50%);
  }
}

.btn-submit:active {
  &:before {
    clip-path: circle(100% at -80% 90%);
  }
}

.btn-submit-text {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.285;
  letter-spacing: 0.04em;
  color: #fff;
  transition: color .3s ease-in-out;
}

@media (max-width: 767.98px) {
  .btn-submit-text {
    line-height: 1.285
  }
}

@media (max-width: 1023.98px) {
  .btn-submit__text {
    line-height: 1.428;
  }
}

.btn-submit-icon {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  overflow: hidden;
  background-color: #fff;
  position: relative;
  margin-left: 7px;
  transition: background-color .3s ease-in-out;
}

@media (max-width: 767.98px) {
  .btn-submit-icon {
    width: 12px;
    height: 12px;
    margin-left: 6px;
  }
}

.btn-submit-icon::before,
.btn-submit-icon::after {
  content: '';
  width: 6px;
  height: 9px;
  position: absolute;
  top: 50%;
  left: 55%;
  background-image: url();
  transition: transform .3s ease-in-out;
}

@media (max-width: 767.98px) {
  .btn-submit-icon::before,
  .btn-submit-icon::after {
    width: 4.2px;
    height: 6px;
    background-image: url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}

.btn-submit-icon::before {
  transform: translate(-260%, -50%);
}

.btn-submit-icon::after {
  transform: translate(-50%, -50%);
}

.spinner {
  display: inline-block;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  border: .215em solid transparent;
  vertical-align: middle;
  font-size: 18px;
  border-top-color: white;
  animation: spin 1s cubic-bezier(.55, .15, .45, .85) infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

@keyframes slide-in-down {
  0% {
    transform: translateY(-20px);
    opacity: 0;
  }

  50% {
    opacity: .8;
  }

  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes slide-out-down {
  0% {
    transform: translateY(0);
    opacity: 1;
  }

  40% {
    opacity: .2;
  }

  100% {
    transform: translateY(20px);
    opacity: 0;
  }
}

.submit-btn-label-enter-active {
  animation: slide-in-down 260ms cubic-bezier(.0, .0, .2, 1);
}

.submit-btn-label-leave-active {
  animation: slide-out-down 260ms cubic-bezier(.4, .0, 1, 1);
}
</style>